
const { category_controller_save,
    category_controller_find, category_controller_delete,
    category_controller_get } = require("../../../controller/product/category/CATEGORY.CONTROLLER");
const { serverError } = require("../../../utils");
const { emptyCheck } = require("../../../utils/checkEmpty");
const { getCateTree, getPermissionConfigTree } = require("../../../utils/getCateTree");
const pageSizeList = require("../../../utils/slicePageSize");

/**
 * @description 分类路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/product/category");

/**@description 添加或编辑 */
router.post("/create", async (ctx, next) => {
    try {
        console.log("发过来的参数body", ctx.request.body)
        const {        // shopId,
            id,
            parentId = undefined,
            cateName = undefined,
            sort = undefined,
            status = undefined
        } = ctx.request.body
        const check = emptyCheck({ cateName })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await category_controller_save({
                id: id,
                parentId, cateName, sort, status
            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})

/**@description 获取分类树 */
router.post("/list", async (ctx, next) => {
    try {
        const list = await category_controller_find({ pageSize: 100, current: 1 });
        const res = getCateTree(list, "0")
        ctx.body = {
            code: 0,
            msg: "查询成功",
            data: [
                {
                    id: "0",
                    cateName: "主分类",
                    parentId: "xx",
                    sort: 1,
                    status: 1,
                    children: res
                }
            ]
        };
    } catch (error) {
        console.log(error);
        ctx.body = serverError
    }
})


/**@description 根据id获取分类详情 */
router.get("/get", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await category_controller_get({ id });
        console.log("查询：\n", id)
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 根据id删除分类 */
router.delete("/delete", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await category_controller_delete({ ids: [id] });
        console.log("查询：\n", id)
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})


/**@description 获取全部权限树配置 */
router.post("/getPermissionConfig", async (ctx, next) => {
    try {
        const tree = await category_controller_find({});
        const list = pageSizeList({ data: tree, pageSize: 100, current: 1 })
        const permissions = getPermissionConfigTree(list, "0")
        ctx.body = {
            status: "0000",
            message: "查询成功",
            data: {
                permissions: permissions
            }
        };
    } catch (error) {
        console.log(error);
        ctx.body = serverError
    }
})

// 查询列表
router.post("/list", async (ctx, next) => {
    try {
        const { current, pageSize, cateName, } = ctx.request.body;
        const res = await category_controller_find({
            current, pageSize, cateName
        })
        const list = pageSizeList({ data: res, pageSize, current })
        ctx.body = {
            status: "0000",
            message: "查询成功",
            data: {
                total: res.length,
                list: list
            }
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }

})


// 删除
router.post("/delete", async (ctx, next) => {
    try {
        const { ids = [] } = ctx.request.body
        if (ids.length === 0) {
            ctx.body = {
                status: '0011',
                message: "未选择需要删除的数据"
            }
        }
        else {
            const res = await category_controller_delete({ ids: ids })
            ctx.body = res;
        }
    } catch (error) {
        ctx.body = serverError
    }
})



module.exports = router